package com.hcy.app.screen.cipher

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hcy.app.utils.SimpleSnowflake
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CipherViewModel
@Inject constructor(
    private val noteDao: NoteDao,
    private val contactDao: ContactDao,
    private val secretDao: SecretDao
): ViewModel(){
    val generator = SimpleSnowflake(workerId = 1)

    private val _cipherState = MutableStateFlow(CipherState())
    val cipherState: StateFlow<CipherState> = _cipherState.asStateFlow()

    private inline fun updateState(transform: CipherState.() -> CipherState) {
        _cipherState.update{ state -> state.transform() }
    }

    init {
        loadInfo()
    }

    // 启动时加载全部数据
    fun loadInfo(){
        viewModelScope.launch {
            noteDao.findAll().collect { li->
                updateState{ copy(noteList=li) }
            }
            secretDao.findAll().collect { li->
                updateState { copy( secretList = li ) }
            }
            contactDao.findAll().collect { li->
                updateState{ copy(contactList=li) }
            }
        }
    }
    // 保存笔记
    suspend fun saveNote(noteItem: NoteItem): Long{
        if ( noteItem.id ==0L ){
            noteItem.id = generator.nextId()
            noteItem.createAt = System.currentTimeMillis()/1000L
            noteDao.insert(noteItem)
        }else{
            noteItem.updateAt = System.currentTimeMillis()/1000L
            noteDao.update(noteItem)
        }
        return noteItem.id
    }

    // 设置
    suspend fun getNoteDetailById(id: Long): NoteItem{
        return noteDao.getById(id)
    }

    // 删除笔记
    fun deleteNote(ids: List<Long>){
        viewModelScope.launch {
            if (ids.isNotEmpty()){
                noteDao.delete(ids)
            }
        }
    }
    // 获取详情
    suspend fun getSecretDetail(id: Long): SecretItem{
        return secretDao.getById(id)
    }
    // 获取密钥关联的笔记title
    suspend fun getBindNoteList(secretId: Long): List<String>{
        return secretDao.getBindNoteList(secretId)
    }

    // 保存密钥信息,如果数据存在就更新
    suspend fun saveSecretKey(item: SecretItem): Long{
        if ( item.id ==0L ){
            item.id = generator.nextId()
            secretDao.insert(item)
        }else{
            secretDao.update(item)
        }
        return item.id
    }

    suspend fun findSecretList():Flow<List<SecretItem>>{
        return secretDao.findAll()
    }




}

data class CipherState(
    val noteList: List<NoteItem> = emptyList(),
    val secretList: List<SecretItem> = emptyList(),
    val contactList: List<ContactItem> = emptyList(),
//    val currNote: NoteItem = NoteItem(0,"","","",0,0,0,0), // 当前正在编辑的笔记
)